package com.lanou3g.musicenjoy.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.RankingAdapter;
import com.lanou3g.musicenjoy.entity.Ranking;
import com.lanou3g.musicenjoy.volley.VolleySingleton;


import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 李宁 on 15/10/20.
 * 排行页面
 */
public class RankingFragment extends Fragment implements AdapterView.OnItemClickListener {

    private String startUrl = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&" +
            "version=5.6.5.0&method=baidu.ting.billboard.billCategory&format=json&kflag=1";
    private VolleySingleton volleySingleton;
    private StringRequest stringRequest;
    private List<Ranking.Content> ranking;//实体类
    private RankingAdapter adapter;
    private ProgressDialog dialog;
    private ListView ranking_listView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ranking, null);
       // ButterKnife.bind(this, view);
        //listView加监听

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ranking_listView = (ListView) getActivity().findViewById(R.id.ranking_listView);
        ranking_listView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        adapter = new RankingAdapter(getActivity());
        this.getUrl();
    }
    public void getUrl() {
        stringRequest = new StringRequest(startUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson =new Gson();

                Ranking result = gson.fromJson(response, Ranking.class);
                if (result != null) {
                    ranking = result.getContent();
                }

                //加载Adapter
                adapter.setData(ranking);
                ranking_listView.setAdapter(adapter);
                //稍等加载后隐藏
                if (dialog.isShowing()){
                    dialog.dismiss();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        //加入到请求列表
        volleySingleton.addQueue(stringRequest);
        //加载中
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("请稍等,乐享音乐加载中...");
        dialog.show();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Ranking.Content content = (Ranking.Content) parent.getItemAtPosition(position);
        //传值
        int type = content.getType();
        String url = content.getPic_s192();

        NewSongListFragment fragment = new NewSongListFragment();
        //跳转
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        bundle.putInt("type",type);
        fragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.container,fragment)
                .commit();


    }
}
